import zoomviewcontroler from './common/zoomviewcontroler.js'
import plotControler from './common/plotcontroler.js'
import mainModdify from './common/modifymaincontroler.js'
import { PanViewControler } from './common/panViewControler.js'
import LGXCOM from '../common/common.js'
import { LGXEnums } from '../common/sysenum.js'


class AbsMainControler {
    constructor(manager, displayMng, mulselsvc) {
        this.dataManger = manager
        this.displayMng = displayMng;
        this.zoomViewControler = new zoomviewcontroler.ZoomViewControler(this.dataManger);
        this.plotControler = new plotControler.PlotControler(this.dataManger);
        this.panViewControler = new PanViewControler(this.dataManger, displayMng);
        this.mainModdifyControler = new mainModdify.ModifyMainControler(this, this.dataManger, mulselsvc);
        this.curentControler = this.mainModdifyControler;
    }

    act2Mouse(map, mosEvnType, preWorldPt) {
        this.dataManger.clearFreshZone();
        var validPt = new LGXCOM.LGXPoint();
        validPt.clone(preWorldPt);

        let preFinish = 0;
        let preCurentGraph = this.dataManger.getCurentGraph();
        if (preCurentGraph) {
            preFinish = preCurentGraph.getFinish();
        }

        let resultState = null;
        resultState = this.curentControler.act2Mouse(mosEvnType, validPt);
        preWorldPt.x = validPt.x;
        preWorldPt.y = validPt.y;
        let pCurentGraph = this.dataManger.getCurentGraph();
        let mode = this.dataManger.getMapWorkMode();
        if (mode == LGXEnums.LGXWorkMode.DrawGraphMode && pCurentGraph && pCurentGraph.getFinish() == 1) {
            if (pCurentGraph != preCurentGraph) {
                this.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
            }
            else if (preFinish == 0) {
                this.setMapWorkMode(LGXEnums.LGXWorkMode.ModifyMode);
            }
        }
        if (mosEvnType.type == LGXEnums.LGXMouseMode.LMouseDown) {
            this.hideAbsorbRayLine(validPt, 1);
            this.hideAbsorbRayLine(validPt, 2);
        }
        this.rePaint(map, 0);
        return resultState;
    }

    actToMouseHover(map, mousePoint) {

    }

    act2MouseWheel(map, delta, mospos, ctrlKeyDown) {
        // let mapInfo = this.dataManger.getMapViewInfo();
        // if (mapInfo.editMode != 'autocad' && !ctrlKeyDown) {
        //     let offsetgap = 20;
        //     if (delta < 0) {
        //         offsetgap = -20;
        //     }
        //     this.displayMng.panView(map, this.dataManger, 0, offsetgap);
        // }
        // else 
        {
            this.zoomViewControler.act2MouseWheel(delta, mospos, ctrlKeyDown);
            this.rePaint(map, 1);
        }
    }

    onKeyPress(map, evt, mospos) {
        var keycode = evt.code;
        if (this.dataManger.getMapWorkMode() == LGXEnums.LGXWorkMode.WanderMode) {
            this.panViewControler.onKeyPress(map, evt, mospos);
        }
        else {
            this.subActToKeyPress(map, evt, mospos);
        }
    }

    subActToKeyPress(map, evt, mospos) { }

    setTool(kind,symbolUUID) {
        this.curentControler = this.plotControler;
        this.dataManger.setTool(kind,symbolUUID);
        this.dataManger.setMapWorkMode(LGXEnums.LGXWorkMode.DrawGraphMode);
    }

    setMapWorkMode(mode) {

    }

    rePaint(map, kind) {
        this.displayMng.rePaint(map, this.dataManger, kind);
    }

    hideAbsorbRayLine(point, kind) {
        let auxDataMng = this.dataManger.getAuxDataManager();
        auxDataMng.resetProcessingEdgeDirLine(point, point, kind, 2, null, null);
        let ptemp = this.dataManger.getYLastAbsorbGraph();
        if (ptemp && ptemp.getUseful() == 1) {
            this.dataManger.recordRefreshScope(ptemp.getZone());
        }

        let graphZoneLst = this.dataManger.setAbsorbRayLine(kind, null);
        for (let i = 0; i < graphZoneLst.length; i++) {
            let rect = graphZoneLst[i];
            this.dataManger.recordRefreshScope(rect);
        }
    }

    finishMultiMode() {

    }

    getCursorBySelGraphKind(painter,mosEvent, screenPt, mousePt) {  
        return 'auto';
    }
}

export default { AbsMainControler }

